package com.alibaba.cirtus.ots.application.adapter.ability.facade.serviceV2.sap;

import com.alibaba.citrus.ots.common.annotation.ProcessInvoker;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsapplicationadaptersdk.commonsendcallback.dto.SendCrmCallbackRequest;
import com.epoch.app.otsapplicationadaptersdk.commonsendcallback.dto.SendSapCallbackRequest;
import com.epoch.app.otsapplicationadaptersdk.commonsendcallback.dto.SendTpmCallbackPermissionRequest;
import com.epoch.app.otsapplicationadaptersdk.commonsendcallback.service.CommonSendCallbackService;
import com.epoch.app.otsapplicationadaptersdk.crm.MnCrmCommResponse;
import com.epoch.app.otsapplicationadaptersdk.sap.MnSapCommResponse;
import com.epoch.app.otsapplicationadaptersdk.service.SapService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.xml.bind.DatatypeConverter;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2022/11/11 11:36
 */
@Component
@Primary
public class CommonSendCallbackServiceImpl implements CommonSendCallbackService {
    private final Log log = Log.getLogger(CommonSendCallbackServiceImpl.class);

    @Resource
    private SapService sapService;

    @Override
    @ProcessInvoker(remark = "consumeMQ_sendSapCallback", errorCode = "OTS-02-007-00-16-999", printLog = true)
    public Result<String> sendSapCallback(SendSapCallbackRequest sendSapCallbackRequest) {
        String message = sendSapCallbackRequest.getMessage();
        if(StringUtils.isBlank(message)){
            log.error("CommonSendCallbackServiceImpl_sendSapCallback_get_message_is_null");
            return Result.success(null);
        }
        MnSapCommResponse mnSapCommResponse = JSONObject.parseObject(message, MnSapCommResponse.class);
        Object result = sapService.restSendCallbackToSap(mnSapCommResponse);
        return Result.success(result.toString());
    }

    @Override
    @ProcessInvoker(remark = "consumeMQ_sendCrmCallback", errorCode = "OTS-02-007-00-16-999", printLog = true)
    public Result<String> sendCrmCallback(SendCrmCallbackRequest sendCrmCallbackRequest) {
        String message = sendCrmCallbackRequest.getMessage();
        if(StringUtils.isBlank(message)){
            log.error("CommonSendCallbackServiceImpl_sendCrmCallback_get_message_is_null");
            return Result.success(null);
        }
        MnCrmCommResponse mnCrmCommResponse = JSONObject.parseObject(message, MnCrmCommResponse.class);
        Object result = sapService.restSendCallbackToCrm(mnCrmCommResponse);
        return Result.success(result.toString());
    }

    public Result sendTpmCallbackPermission(SendTpmCallbackPermissionRequest sendTpmCallbackPermissionRequest){
        Map<String ,String > resultMap  = new HashMap<>();
        try{
            String accessKey = sendTpmCallbackPermissionRequest.getAk();
            String secretKey = sendTpmCallbackPermissionRequest.getSk();
            long timestamp = System.currentTimeMillis();
            String signStr = timestamp + accessKey + secretKey;
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            byte[] digest = md5.digest(signStr.getBytes(StandardCharsets.UTF_8));
            String sign = DatatypeConverter.printHexBinary(digest).toLowerCase();


            resultMap.put("ak",accessKey);
            resultMap.put("timestamp",Long.toString(timestamp));
            resultMap.put("token",sign);
        }catch (Exception e){
            log.error("sendTpmCallbackPermission ：{}",e.getMessage());
        }

        return Result.success(resultMap);
    }
}
